<template>
  <el-aside :width="width" class="aside-container">
    <el-menu
        :default-active="activePath"
        :collapse="isCollapse"
        :collapse-transition="false"
        style="border: none"
    >
      <h3 v-show="!isCollapse" style="margin-left: 10px">{{ $t('system') }}</h3>
      <h3 v-show="isCollapse" style="margin-left: 10px">{{ $t('systemShort') }}</h3>
      <el-menu-item
          v-for="item in noChildren"
          :key="item.path"
          :index="item.path"
          @click="navigate(item)"
      >
        <component class="icons" :is="item.icon"></component>
        <span>{{ $t(item.titleKey) }}</span>
      </el-menu-item>
      <el-sub-menu
          v-for="item in hasChildren"
          :key="item.path"
          :index="item.path"
      >
        <template #title>
          <component class="icons" :is="item.icon"></component>
          <span>{{ $t(item.titleKey) }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item
              v-for="subItem in item.children"
              :index="subItem.path"
              :key="subItem.path"
              @click="navigate(subItem)"
          >
            <span>{{ $t(subItem.titleKey) }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<script setup>
import { useRoute, useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
import menuConfig from '../utils/menuConfig.js';
import { useHomeDataStore } from '../stores/index.js';
import { computed, ref, onMounted } from 'vue';

const { t } = useI18n();
const route = useRoute();
const router = useRouter();
const activePath = ref(route.path);
const store = useHomeDataStore();

// 确保isCollapse是响应式的
const isCollapse = computed(() => store.state.isCollapse);

// 根据折叠状态计算宽度
const width = computed(() => isCollapse.value ? '64px' : '200px');

// 导航处理函数
const navigate = (menu) => {
  try {
    // 使用路由名称导航
    router.push({ name: menu.name });

    // 确保传递完整的菜单信息，包括 titleKey
    const menuWithTitle = {
      name: menu.name,
      path: menu.path,
      titleKey: menu.titleKey
    };
    store.selectMenu(menuWithTitle);
    activePath.value = menu.path;
  } catch (error) {
    console.error('导航错误:', error);
    // 如果路由名称导航失败，尝试使用路径
    try {
      router.push(menu.path);
      store.selectMenu({
        name: menu.name,
        path: menu.path,
        titleKey: menu.titleKey
      });
      activePath.value = menu.path;
    } catch (pathError) {
      console.error('路径导航也失败:', pathError);
    }
  }
};

// 分类菜单项
const noChildren = computed(() => menuConfig.filter(item => !item.children));
const hasChildren = computed(() => menuConfig.filter(item => item.children && item.children.length > 0));

// 组件挂载时更新激活路径
onMounted(() => {
  activePath.value = route.path;
});
</script>

<style scoped lang="less">
.aside {
  height: 100vh;
  background: linear-gradient(180deg, #304156 0%, #2c3e50 100%);
  transition: width 0.3s ease;
  position: relative;

  &::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 1px;
    background: linear-gradient(180deg, transparent, rgba(255, 255, 255, 0.1), transparent);
  }
}

/* 菜单容器 */
.el-menu {
  border: none !important;
  background: transparent !important;
  width: 100%;
}

/* 菜单项基础样式 */
.el-menu-item,
:deep(.el-sub-menu__title) {
  color: rgba(255, 255, 255, 0.8) !important;
  background-color: transparent !important;
  border-bottom: none !important;
  height: 54px;
  line-height: 54px;
  padding: 0 24px;
  margin: 2px 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background: #409eff;
    transform: scaleY(0);
    transition: transform 0.3s ease;
  }
}

/* 激活状态的菜单项 */
.el-menu-item.is-active {
  color: #409eff !important;
  background: linear-gradient(135deg, rgba(64, 158, 255, 0.1) 0%, rgba(64, 158, 255, 0.05) 100%) !important;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.2);

  &::before {
    transform: scaleY(1);
  }
}

/* 悬停状态 */
.el-menu-item:hover,
:deep(.el-sub-menu__title:hover) {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%) !important;
  color: white !important;
  transform: translateX(4px);
}

/* 子菜单项样式 */
:deep(.el-menu-item-group__list .el-menu-item) {
  color: rgba(255, 255, 255, 0.7) !important;
  background-color: transparent !important;
  padding-left: 48px !important;
  height: 48px;
  line-height: 48px;
  margin: 1px 8px;
  font-size: 13px;

  &::before {
    left: 24px;
    width: 2px;
  }
}

:deep(.el-menu-item-group__list .el-menu-item.is-active) {
  color: #409eff !important;
  background: linear-gradient(135deg, rgba(64, 158, 255, 0.08) 0%, rgba(64, 158, 255, 0.04) 100%) !important;
}

:deep(.el-menu-item-group__list .el-menu-item:hover) {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.08) 0%, rgba(255, 255, 255, 0.04) 100%) !important;
  color: rgba(255, 255, 255, 0.9) !important;
}

/* 子菜单展开时的背景 */
:deep(.el-sub-menu.is-opened > .el-sub-menu__title) {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%) !important;
  color: white !important;
}

/* 图标统一样式 */
.icons {
  margin-right: 12px;
  font-size: 18px !important;
  width: 18px !important;
  height: 18px !important;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

/* 确保所有图标大小一致 */
.icons svg {
  width: 18px !important;
  height: 18px !important;
}

/* 标题样式 */
h3 {
  color: white;
  text-align: center;
  margin: 24px 0;
  font-size: 18px;
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  position: relative;

  &::after {
    content: '';
    position: absolute;
    bottom: -8px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 2px;
    background: linear-gradient(90deg, transparent, #409eff, transparent);
  }
}

/* 折叠状态下的样式调整 */
.el-menu--collapse {
  .el-menu-item,
  :deep(.el-sub-menu__title) {
    padding: 0 !important;
    text-align: center;
    justify-content: center;

    &::before {
      display: none;
    }
  }

  .icons {
    margin-right: 0 !important;
  }

  h3 {
    font-size: 0;

    &::after {
      display: none;
    }
  }
}

/* 移除默认的边框和阴影 */
:deep(.el-menu-item),
:deep(.el-sub-menu__title),
:deep(.el-menu-item-group__list .el-menu-item) {
  border: none !important;
  box-shadow: none !important;
}

/* 子菜单组标题隐藏 */
:deep(.el-menu-item-group__title) {
  display: none;
}

/* 确保子菜单背景色正确 */
:deep(.el-menu--inline) {
  background-color: transparent !important;
}

/* 修复子菜单项的背景色问题 */
:deep(.el-sub-menu .el-menu) {
  background-color: transparent !important;
}

:deep(.el-sub-menu .el-menu .el-menu-item) {
  background-color: transparent !important;
}

/* 确保选中状态的样式优先级 */
:deep(.el-menu-item.is-active),
:deep(.el-sub-menu .el-menu .el-menu-item.is-active) {
  background: linear-gradient(135deg, rgba(64, 158, 255, 0.1) 0%, rgba(64, 158, 255, 0.05) 100%) !important;
  color: #409eff !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .el-menu-item,
  :deep(.el-sub-menu__title) {
    height: 48px;
    line-height: 48px;
    padding: 0 16px;
    margin: 1px 4px;
  }

  :deep(.el-menu-item-group__list .el-menu-item) {
    padding-left: 32px !important;
    height: 44px;
    line-height: 44px;
  }

  .icons {
    font-size: 16px !important;
    width: 16px !important;
    height: 16px !important;
  }

  h3 {
    font-size: 16px;
    margin: 16px 0;
  }
}
</style>
